<?php
function find_parent($cat,$parent_id)
{
    if($parent_id == 0)
        return true;
    foreach($cat as $v)
    {
        if($v->id == $parent_id)
        {   
          return true;
        }
    }
    return false;
}

function recu($cat,$parent_id,$lv, $selected = null)
{
    foreach($cat as $v)
    {
        if(!find_parent($cat,$v->parent_id))
            $v->parent_id = 0;
        
        $s = $parent_id;
        $x = $lv;
        $sl = '';
        if($v->parent_id == $parent_id)
        {
            $lv++;
            $i = 0;
            $str = '';
            while($i < $lv)
            {
                if(!$str)
                    $str = '|_';
                $str = '&nbsp&nbsp&nbsp'.$str;
                $i++;
            }
            if($v->id == $selected)
            $sl = 'selected';
            echo '<option value="'.$v->id.'" '.$sl.'>'.$str.$v->title.'</option>';
            $parent_id = $v->id;
            recu($cat,$parent_id,$lv,$selected);
        }
        $lv = $x;
        $parent_id = $s;
        //echo "<br />";
    }
}
?>
<form method="POST" action="<?php echo base_url().'index.php/category/add'?>">
<input type="text" name="title" value="<?php if(!empty($item)) echo $item->title; ?>" />
<?php if(!empty($item)): ?>
<input type="hidden" name="id" value="<?php echo $item->id;?>" />
<?php endif;?>
<select name="parent_id">
    <option value="0">.....</option>
    <?php
        if(!empty($item))
        {
            recu($category,0,-1,$item->parent_id);
        }else{
            recu($category,0,-1);
        }    
    
    ?>
</select>
<input type="submit" name="submit" value="add" />
</form>